package cn.outsourcing.supermarket.po;

import cn.outsourcing.supermarket.common.constants.PayConstants;
import cn.outsourcing.supermarket.common.enums.ChannelEnum;
import cn.outsourcing.supermarket.common.enums.OrderTypeEnum;
import cn.outsourcing.supermarket.common.utils.DataUtils;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 订单实体类
 *
 * @author gzkemays
 * @since 2021-10-14
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class HiolabsOrder implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @TableField("order_sn")
    private String orderSn;

    private Integer userId;

    /**
     * 101：未付款、102：已取消、103已取消(系统)、201：已付款、202：订单取消，退款中、203：已退款、301：已发货、302：已收货、303：已收货(系统)、401：已完成、801：拼团中,未付款、802：拼团中,已付款
     */
    private Integer orderStatus;

    /**
     * 线下支付订单标志
     */
    private Boolean offlinePay;

    /**
     * 0未发货，1已发货
     */
    private Boolean shippingStatus;

    private Boolean printStatus;

    private Boolean payStatus;

    private String consignee;

    private Integer country;

    private Integer province;

    private Integer city;

    private Integer district;

    private String address;

    private String printInfo;

    private String mobile;

    private String postscript;

    private String adminMemo;

    /**
     * 免邮的商品的邮费，这个在退款时不能退给客户
     */
    private BigDecimal shippingFee;

    private String payName;

    private String payId;

    /**
     * 0没改价，不等于0改过价格，这里记录原始的价格
     */
    private BigDecimal changePrice;

    /**
     * 实际需要支付的金额
     */
    private BigDecimal actualPrice;

    /**
     * 订单总价
     */
    private BigDecimal orderPrice;

    /**
     * 商品总价
     */
    private BigDecimal goodsPrice;

    private Integer addTime;

    /**
     * 付款时间
     */
    private Integer payTime;

    /**
     * 发货时间
     */
    private Integer shippingTime;

    /**
     * 确认时间
     */
    private Integer confirmTime;

    /**
     * 成交时间，用户评论或自动好评时间
     */
    private Integer dealdoneTime;

    /**
     * 配送费用
     */
    private Integer freightPrice;

    /**
     * 顺丰保价金额
     */
    private BigDecimal expressValue;

    private String remark;

    /**
     * 订单类型：0普通，1秒杀，2团购，3返现订单,7充值，8会员
     * @see OrderTypeEnum
     */
    private Integer orderType;

    /**
     * 订单删除标志
     */
    private Integer isDelete;
    /**
     * 支付渠道
     * @see ChannelEnum
     */
    private Integer channel;



    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int dayOfMonth = localDateTime.getMonthValue();
        String localDate = new Date().toString();
        System.out.println("year" + year);
        System.out.println("dayOfMonth" + dayOfMonth);
        System.out.println("localDate" + localDate);
    }


    public static HiolabsOrder creatOrderByCart(List<HiolabsCart> hiolabsCarts, HiolabsUser userMsg,
                                                Boolean isIntegral,Integer channel) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int dayOfMonth = localDateTime.getMonthValue();
        long milli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        String orderSn = "" + year + dayOfMonth + milli;

        BigDecimal reduce = hiolabsCarts.stream()
                .map(e -> e.getAddPrice().multiply(new BigDecimal(e.getNumber())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // FIXME：没有考虑当积分兑换数量大于金额时的情况
//        BigDecimal integral = Optional.ofNullable(userMsg.getCredits())
//                .map(e -> e.divide(BigDecimal.valueOf(10), 2, RoundingMode.HALF_UP))
//                .orElse(BigDecimal.ZERO);
        // 获取最大可用积分
        BigDecimal useIntegral = DataUtils.getMaxIntegral(reduce, userMsg.getCredits());
        BigDecimal actualPrice = Objects.equals(isIntegral, true) ? reduce.subtract(DataUtils.getPriceByIntegral(useIntegral)) : reduce;
        HiolabsOrder hiolabsOrder = new HiolabsOrder();
//        hiolabsOrder.setId(userMsg.getId());
        hiolabsOrder.setOrderSn(orderSn);
        hiolabsOrder.setUserId(userMsg.getId());
        hiolabsOrder.setOrderStatus(PayConstants.NOT_PAYMENT);
        hiolabsOrder.setOfflinePay(false);
        hiolabsOrder.setShippingStatus(false);
        hiolabsOrder.setPrintStatus(false);
        hiolabsOrder.setPayStatus(false);
        hiolabsOrder.setAddTime(DataUtils.getSeconds());
//        hiolabsOrder.setConsignee(userMsg.getUsername());
//        hiolabsOrder.setCountry(userMsg.getCountry());
//        hiolabsOrder.setProvince(userMsg.getProvince());
//        hiolabsOrder.setCity(userMsg.getCity());
//        hiolabsOrder.setDistrict();
//        hiolabsOrder.setAddress();
//        hiolabsOrder.setPrintInfo();
        hiolabsOrder.setMobile(userMsg.getMobile());
//        hiolabsOrder.setPostscript();
//        hiolabsOrder.setAdminMemo();
//        hiolabsOrder.setShippingFee();
//        hiolabsOrder.setPayName();
//        hiolabsOrder.setPayId();
        hiolabsOrder.setActualPrice(actualPrice);
        hiolabsOrder.setChangePrice(reduce);
        hiolabsOrder.setOrderPrice(reduce);
        hiolabsOrder.setGoodsPrice(reduce);
//        hiolabsOrder.setAddTime();
//        hiolabsOrder.setPayTime();
//        hiolabsOrder.setShippingTime();
//        hiolabsOrder.setConfirmTime();
//        hiolabsOrder.setDealdoneTime();
//        hiolabsOrder.setFreightPrice();
//        hiolabsOrder.setExpressValue();
//        hiolabsOrder.setRemark();
        hiolabsOrder.setOrderType(0);
//        hiolabsOrder.setIsDelete();
        hiolabsOrder.setChannel(channel);
        return hiolabsOrder;
    }

    public static List<HiolabsOrderGoods> creatOrderDedailByCart(List<HiolabsCart> hiolabsCarts,
                                                                 HiolabsOrder hiolabsOrder) {
        List<HiolabsOrderGoods> hiolabsOrderGoodslist = Lists.newArrayList();
        for (HiolabsCart hiolabsCart : hiolabsCarts) {
            hiolabsOrderGoodslist.add(convertFromHiolabsCart(hiolabsCart, hiolabsOrder));
        }
        return hiolabsOrderGoodslist;
    }

    private static HiolabsOrderGoods convertFromHiolabsCart(
            HiolabsCart hiolabsCart, HiolabsOrder hiolabsOrder) {
        HiolabsOrderGoods hiolabsOrderGoods = new HiolabsOrderGoods();
        hiolabsOrderGoods.setOrderId(hiolabsOrder.getId());
        hiolabsOrderGoods.setGoodsId(hiolabsCart.getGoodsId());
        hiolabsOrderGoods.setGoodsName(hiolabsCart.getGoodsName());
        hiolabsOrderGoods.setGoodsAka(hiolabsCart.getGoodsAka());
        hiolabsOrderGoods.setProductId(hiolabsCart.getProductId());
        hiolabsOrderGoods.setNumber(hiolabsCart.getNumber());
        hiolabsOrderGoods.setRetailPrice(hiolabsCart.getRetailPrice());
        hiolabsOrderGoods.setGoodsSpecifitionNameValue(hiolabsCart.getGoodsSpecifitionNameValue());
        hiolabsOrderGoods.setGoodsSpecifitionIds(hiolabsCart.getGoodsSpecifitionIds());
        hiolabsOrderGoods.setListPicUrl(hiolabsCart.getListPicUrl());
        hiolabsOrderGoods.setUserId(hiolabsCart.getUserId());
        return hiolabsOrderGoods;
    }

    public static HiolabsOrder creatOrderByBalance(BigDecimal balance, HiolabsUser userMsg) {
        LocalDateTime localDateTime = LocalDateTime.now();
        int year = localDateTime.getYear();
        int dayOfMonth = localDateTime.getMonthValue();
        long milli = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        String orderSn = "" + year + dayOfMonth + milli;

        BigDecimal reduce = Optional.ofNullable(balance).orElse(BigDecimal.ZERO);
        // FIXME：没有考虑当积分兑换数量大于金额时的情况
//        BigDecimal integral = Optional.ofNullable(userMsg.getCredits())
//                .map(e -> e.divide(BigDecimal.valueOf(10), 2, RoundingMode.HALF_UP))
//                .orElse(BigDecimal.ZERO);
        // 获取最大可用积分
        BigDecimal useIntegral = DataUtils.getMaxIntegral(reduce, userMsg.getCredits());
        HiolabsOrder hiolabsOrder = new HiolabsOrder();
//        hiolabsOrder.setId(userMsg.getId());
        hiolabsOrder.setOrderSn(orderSn);
        hiolabsOrder.setUserId(userMsg.getId());
        hiolabsOrder.setOrderStatus(PayConstants.NOT_PAYMENT);
        hiolabsOrder.setOfflinePay(false);
        hiolabsOrder.setShippingStatus(false);
        hiolabsOrder.setPrintStatus(false);
        hiolabsOrder.setPayStatus(false);
        hiolabsOrder.setAddTime(DataUtils.getSeconds());
//        hiolabsOrder.setConsignee(userMsg.getUsername());
//        hiolabsOrder.setCountry(userMsg.getCountry());
//        hiolabsOrder.setProvince(userMsg.getProvince());
//        hiolabsOrder.setCity(userMsg.getCity());
//        hiolabsOrder.setDistrict();
//        hiolabsOrder.setAddress();
//        hiolabsOrder.setPrintInfo();
        hiolabsOrder.setMobile(userMsg.getMobile());
//        hiolabsOrder.setPostscript();
//        hiolabsOrder.setAdminMemo();
//        hiolabsOrder.setShippingFee();
//        hiolabsOrder.setPayName();
//        hiolabsOrder.setPayId();
//        hiolabsOrder.setChangePrice();
        if (balance.compareTo(BigDecimal.ZERO) != 0) {
            hiolabsOrder.setActualPrice(balance);
        } else {
            hiolabsOrder.setActualPrice(reduce);
        }
        hiolabsOrder.setOrderPrice(reduce);
        hiolabsOrder.setGoodsPrice(reduce);
        hiolabsOrder.setChangePrice(reduce);
//        hiolabsOrder.setAddTime();
//        hiolabsOrder.setPayTime();
//        hiolabsOrder.setShippingTime();
//        hiolabsOrder.setConfirmTime();
//        hiolabsOrder.setDealdoneTime();
//        hiolabsOrder.setFreightPrice();
//        hiolabsOrder.setExpressValue();
//        hiolabsOrder.setRemark();
        hiolabsOrder.setOrderType(OrderTypeEnum.TOP_UP_BALANCE.getIndex());
//        hiolabsOrder.setIsDelete();
        hiolabsOrder.setChannel(ChannelEnum.WECHAT_JSAPI.getIndex());
        return hiolabsOrder;
    }
}

